<?php

namespace app\home\service;

use app\common\helpers\PayHelper;
use app\home\model\UserOrder;
use think\Config;

class Pay3011 implements IPay
{
    private $channel = null;
    private $key = null;
    private $helper = null;

    public function __construct($config)
    {
        $this->channel = $config['channel'];
        $this->key = $config['key'];
        $this->helper = PayHelper::getInstance($config['channel']);
    }

    public function createOrder($params)
    {
//        if (empty($params['userid']) || empty($params['channel']) || empty($params['stroeid'])) {
//            return false;
//        }
//
//        // 计费点映射关系
//        $map_class = Config::get('MapPayClass');
//        if(!isset($map_class[$params['channel']])){
//            $this->helper->record('channel map not found');
//            return false;
//        }
//
//        $orderModel = new UserOrder();
//        $data = $orderModel->create_order_old($params['userid'], $params['channel'], $params['stroeid'], 5,null);
//        $info = json_decode($data, true);
//
//        $data_Arr['code'] = 'success';
//        $data_Arr['data'] = $info;
//
//        return $data_Arr;
    }
    public function processNotify($params)
    {
        $split_str = str_repeat('-',80);
        $this->helper->record($split_str);
        $this->helper->record($params);

        if(empty($params['out_trade_no']) || empty($params['sign']) || empty($params['extend']) || empty($params['price'])){
            $this->helper->record('some params empty');
            return 'params empty';
        }

        // 验证签名
        if($this->verifySign($params) == false){
            $this->helper->record('verify fail');
            return 'verify fail';
        }
        $this->helper->record('verify ok');

        // 发货
        $time = date('Y-m-d H:i:s', time());
        if($params['pay_status'] == 1){
            $re = Db_connect_server_pay_domian($params['extend'], $params['price'], $time);
            if ($re != 'success') {
                $this->helper->record('delivery fail');
                return 'delivery fail';
            }

            return 'success';
        }

        return 'fail';
    }

    protected function verifySign($params){

        $out_trade_no = $params['out_trade_no'];
        $price = $params['price'];
        $pay_status = $params['pay_status'];
        $extend = $params['extend'];

        $sign = $params['sign'];

        $signStr = md5($out_trade_no.$price.$pay_status.$extend.$this->key);
        if($sign != $signStr){
            return false;
        }

        return true;
    }



}
